package com.my.user.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import com.my.user.entity.User;

import util.ConnManager;

public class UserDaoImpl implements UserDao {

	@Override
	public boolean login(User user) {
		boolean flag=false;
		try{
			Connection conn=ConnManager.getConnection();
			String sql="insert into user (u_name,u_password,u_sex,u_birthday,u_address,u_phone_number,u_qq_number,u_e_mail) values(?,?,?,?,?,?,?,?)";
			PreparedStatement pst=conn.prepareStatement(sql);
			pst.setString(1,user.getuName() );
			pst.setString(2,user.getuPassword() );
			pst.setString(3,user.getuSex() );
			java.sql.Date date=new java.sql.Date(user.getuBirthday().getTime());
			pst.setDate(4,date);
			pst.setString(5,user.getuAddress() );
			pst.setString(6,user.getuPhoneNumber() );
			pst.setString(7,user.getuQqNumber() );
			pst.setString(8,user.getuEMail() );
			pst.execute();
			pst.close();
			conn.close();
			flag=true;
		}catch(Exception e){
			flag=false;
			System.out.println("插入数据出错"+e.getMessage());
		}
		return flag;
	}

	@Override
	public User selectOne(String name) {
		User user1=null;
		try{
			Connection conn=ConnManager.getConnection();
			String sql="select * from user where u_name=?";
			PreparedStatement pst=conn.prepareStatement(sql);
			pst.setString(1,name);
			ResultSet rs=pst.executeQuery();
			System.out.println("123"+name);
			if(rs.next()){
				user1=new User();
				user1.setuId(rs.getInt("u_id"));
				user1.setuName(rs.getString("u_name"));
				user1.setuPassword(rs.getString("u_password"));
				user1.setuSex(rs.getString("u_sex"));
			}else{
				user1=null;
			}
			rs.close();
			pst.close();
			conn.close();
			return user1;
		}catch(Exception e){
			System.out.println("通过用户名查找出错"+e.getMessage());
			return user1;
		}
	}

	@Override
	public User selectOneById(int id) {
		User user=null;
		try {
			Connection conn = ConnManager.getConnection();
			String sql="select u_name,u_password,u_sex,u_birthday,u_address,u_phone_number,u_qq_number,u_e_mail from user where u_id=?";
			PreparedStatement pst=conn.prepareStatement(sql);
			pst.setInt(1, id);
			ResultSet rs=pst.executeQuery();
			if(rs.next()){
				user=new User();
				user.setuId(id);
				user.setuName(rs.getString("u_name"));
				user.setuPassword(rs.getString("u_password"));
				user.setuSex(rs.getString("u_password"));
				user.setuBirthday( rs.getTimestamp("u_birthday") );
				user.setuAddress(rs.getString("u_address"));
				user.setuPhoneNumber(rs.getString("u_phone_number"));
				user.setuQqNumber(rs.getString("u_qq_number"));
				user.setuEMail(rs.getString("u_e_mail"));
			}
			pst.close();
			conn.close();
			return user;
		} catch (Exception e) {
			System.out.println("查询数据出错"+e.getMessage());
			return null;
		}
	}

	@Override
	public boolean updateOne(User user) {
		boolean flag=false;
		try{
			Connection conn=ConnManager.getConnection();
			String sql="update user set u_name=?,u_password=?,u_birthday=?,u_address=?,u_phone_number=?,u_qq_number=?,u_e_mail=? where u_id=?";
			PreparedStatement pst=conn.prepareStatement(sql);
			pst.setString(1, user.getuName());
			pst.setString(2, user.getuPassword());
			java.sql.Date date=new java.sql.Date(user.getuBirthday().getTime());
			pst.setDate(3, date);
			pst.setString(4, user.getuAddress());
			pst.setString(5, user.getuPhoneNumber());
			pst.setString(6, user.getuQqNumber());
			pst.setString(7, user.getuEMail());
			pst.setInt(8,user.getuId());
			pst.execute();
			pst.close();
			conn.close();
			flag=true;
		}catch(Exception e){
			System.out.println("修改数据出错"+e.getMessage());
			flag=false;
		}
		return flag;
	}

}
